setwd("/Users/mdr/Dropbox/stats2/6DataVisualization")
library(foreign)
r4<-read.dta("merged_r4_data.dta")

#convert country to string variable
r4$country<-as.character(r4$country)

# subset each data.frame to just the country & radio question.
r4<-subset(r4,select=c("Q12A", "Q12B", "Q12C", "country"))

#code radio news consumption
table(r4$Q12A,exclude=NULL)
class(r4$Q12A)
r4$radio.2010<-as.numeric(ordered(r4$Q12A))
table(r4$radio.2010)
r4$radio.2010<-ifelse(r4$radio.2010==1|r4$radio.2010==7,NA,r4$radio.2010) 
#factor had sevel levels, so numeric variable is now 1 to 7, with 1 missing and 7 Don't know
r4$radio.2010<-r4$radio.2010-2
table(r4$radio.2010, exclude=NULL)

r4$daily.radio<-ifelse(r4$radio.2010==4,1,0)
table(r4$daily.radio, exclude=NULL) # check that I did not recode NA's

#code tv news consumption
table(r4$Q12B,exclude=NULL)
class(r4$Q12B)
r4$tv.2010<-as.numeric(ordered(r4$Q12B))
table(r4$tv.2010)
r4$tv.2010<-ifelse(r4$tv.2010==1|r4$tv.2010==7,NA,r4$tv.2010)
r4$tv.2010<-r4$tv.2010-2
table(r4$tv.2010, exclude=NULL)

r4$daily.tv<-ifelse(r4$tv.2010==4,1,0)
table(r4$daily.tv, exclude=NULL) # check that I did not recode NA's

#code newspaper news consumption
table(r4$Q12C,exclude=NULL)
class(r4$Q12C)
r4$newspaper.2010<-as.numeric(ordered(r4$Q12C))
table(r4$newspaper.2010)
r4$newspaper.2010<-ifelse(r4$newspaper.2010==1|r4$newspaper.2010==7,NA,r4$newspaper.2010)
r4$newspaper.2010<-r4$newspaper.2010-2
table(r4$newspaper.2010, exclude=NULL)

r4$daily.newspaper<-ifelse(r4$newspaper.2010==4,1,0)
table(r4$daily.newspaper, exclude=NULL) # check that I did not recode NA's

#create country proportion variable
country<-unique(r4$country) #extract unique country names

#create data frame for country level data
map<-as.data.frame(country)
map$radio.prop<-NA
map$tv.prop<-NA
map$newspaper.prop<-NA

#loop to calculate the proportions
for(i in 1:nrow(map)){
	map[i,2]<-mean(r4$daily.radio[r4$country==map[i,1]],na.rm=T)
	map[i,3]<-mean(r4$daily.tv[r4$country==map[i,1]],na.rm=T)
	map[i,4]<-mean(r4$daily.newspaper[r4$country==map[i,1]],na.rm=T)
}

#create fake data for scale
class(map$country)
map$country<-as.character(map$country)
map[21,1]<-"United States of America"
map[22,1]<-"Mexico"
map[21,2:4]<-1
map[22,2:4]<-0
map[23,1]<-"Congo"

library("rworldmap")

sPDF<-joinCountryData2Map(map, joinCode="NAME",nameJoinColumn="country")

#create vector of cutpoints for the legend
c<-seq(from=0, to=1, by=.10)

#create color palatte for legend
library("RColorBrewer")
color<-brewer.pal(n=9,name="Greys")

#radio map
pdf("radio.pdf",height=6)
map<-mapCountryData(sPDF, nameColumnToPlot="radio.prop", missingCountryCol=NA, numCats=11,catMethod=c, mapRegion="Africa", colourPalette=color, mapTitle=NULL,addLegend=T)
dev.off()

#tv map
pdf("tv.pdf",height=6)
map<-mapCountryData(sPDF, nameColumnToPlot="tv.prop", missingCountryCol=NA, numCats=11,catMethod=c, mapRegion="Africa", colourPalette=color, mapTitle=NULL,addLegend=T)
dev.off()

#newspaper map
pdf("newspaper.pdf",height=6)
map<-mapCountryData(sPDF, nameColumnToPlot="newspaper.prop", missingCountryCol=NA, numCats=11,catMethod=c, mapRegion="Africa", colourPalette=color, mapTitle=NULL,addLegend=T)
dev.off()
	